package pl.edu.agh.iosr.gamblingzone.dao.hibernate;

import java.util.Date;
import java.util.List;

import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import pl.edu.agh.iosr.gamblingzone.dao.BettingSlipDAO;
import pl.edu.agh.iosr.gamblingzone.model.BettingSlip;
import pl.edu.agh.iosr.gamblingzone.model.User;

/**
 * The Class BettingSlipDAOBean.
 */
public class BettingSlipDAOBean extends GenericDAOBean<BettingSlip, Long> 
							implements BettingSlipDAO {

	/* (non-Javadoc)
	 * @see pl.edu.agh.iosr.gamblingzone.dao.BettingSlipDAO#getBettingSlipsBetween(pl.edu.agh.iosr.gamblingzone.model.User, java.util.Date, java.util.Date, int)
	 */
	public List<BettingSlip> getBettingSlipsBetween(User user, Date dateBegin, Date dateEnd, int page) {
		
		return findByCriteria(new ComplexCriterion()
				.add(Restrictions.like("owner", user))
				.add(Restrictions.ge("creationDate", dateBegin))
				.add(Restrictions.le("creationDate", dateEnd))
				.add(Order.asc("creationDate")), page);
	}

	/* (non-Javadoc)
	 * @see pl.edu.agh.iosr.gamblingzone.dao.BettingSlipDAO#getPageCount(pl.edu.agh.iosr.gamblingzone.model.User)
	 */
	public int getPageCount(User user) {		
		return getPageCount(new ComplexCriterion().add(Restrictions.like("owner", user)));
	}

	/* (non-Javadoc)
	 * @see pl.edu.agh.iosr.gamblingzone.dao.BettingSlipDAO#findAll(pl.edu.agh.iosr.gamblingzone.model.User, int)
	 */
	public List<BettingSlip> findAll(User user, int page) {
		return findByCriteria(new ComplexCriterion().add(Restrictions.like("owner", user)), page);
	}

	/* (non-Javadoc)
	 * @see pl.edu.agh.iosr.gamblingzone.dao.BettingSlipDAO#getEntitiesCount(pl.edu.agh.iosr.gamblingzone.model.User)
	 */
	public int getEntitiesCount(User user) {
		return getEntitiesCount(new ComplexCriterion().add(Restrictions.like("owner", user)));
	}

	/* (non-Javadoc)
	 * @see pl.edu.agh.iosr.gamblingzone.dao.BettingSlipDAO#getBettingSlipsBetweenCount(pl.edu.agh.iosr.gamblingzone.model.User, java.util.Date, java.util.Date)
	 */
	public int getBettingSlipsBetweenCount(User user,
			Date dateBegin, Date dateEnd) {
		
		return getEntitiesCount(new ComplexCriterion()
			.add(Restrictions.like("owner", user))
			.add(Restrictions.ge("creationDate", dateBegin))
			.add(Restrictions.le("creationDate", dateEnd))
			.add(Projections.rowCount()));
	}

	/* (non-Javadoc)
	 * @see pl.edu.agh.iosr.gamblingzone.dao.BettingSlipDAO#getBettingSlipsBetweenPageCount(pl.edu.agh.iosr.gamblingzone.model.User, java.util.Date, java.util.Date)
	 */
	public int getBettingSlipsBetweenPageCount(User user,
			Date dateBegin, Date dateEnd) {
		
		return getPageCount(new ComplexCriterion()
			.add(Restrictions.like("owner", user))
			.add(Restrictions.ge("creationDate", dateBegin))
			.add(Restrictions.le("creationDate", dateEnd))
			.add(Order.asc("creationDate")));
	}
}
